import { ProductService } from '../../services/product_service'
import uploadFile from '../../utils/uploadImage'
import wxdata from '../../utils/wxdata'
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast'

var free

Page({
  data: {
    autoplay: true,
    interval: 3000,
    product: {},
  },

  onLoad(query) {
    free = query.free
    this.getProductDetail(query.productId, query.width, query.height)
  },

  getProductDetail(id, width, height) {
    let productService = new ProductService()
    productService.getProductById(id).then((res) => {
      if (res.data.category === 'custom') {
        let product = res.data
        product.width = width
        product.height = height
        this.setData({
          product,
        })
      } else {
        this.setData({
          product: res.data,
        })
      }
      wxdata.browse(this.data.product)
    })
  },

  selectPhoto() {
    wx.chooseMedia({
      sourceType: ['album'],
      mediaType: ['image'],
      sizeType: ['original'],
      count: 1,
      success: async (res) => {
        Toast.loading({
          message: '上传中',
          forbidClick: true,
        })
        let tempFilePath = res.tempFiles[0].tempFilePath
        const originalImageUrl = await uploadFile(tempFilePath).catch((err) => {
          console.log(err)
          wx.showToast({
            title: '上传图片失败',
            icon: 'error',
          })
          return
        })
        if (originalImageUrl) {
          if (this.data.product.category === 'custom') {
            wx.navigateTo({
              url: `/pages/photoDetect/photoDetect?originalImagePath=${tempFilePath}&releatedId=${this.data.product.releatedId}&productId=${this.data.product.id}&originalImageUrl=${originalImageUrl}&height=${this.data.product.height}&width=${this.data.product.width}&free=${free}&category=${this.data.product.category}`,
            })
          } else {
            wx.navigateTo({
              url: `/pages/photoDetect/photoDetect?originalImagePath=${tempFilePath}&releatedId=${this.data.product.releatedId}&productId=${this.data.product.id}&originalImageUrl=${originalImageUrl}&category=${this.data.product.category}`,
            })
          }
        }
      },
    })
  },

  takePhoto() {
    if (this.data.product.category === 'custom') {
      wx.navigateTo({
        url: `/pages/camera/camera?productId=${this.data.product.id}&releatedId=${this.data.product.releatedId}&height=${this.data.product.height}&width=${this.data.product.width}&free=${free}&category=${this.data.product.category}`,
      })
    } else {
      wx.navigateTo({
        url: `/pages/camera/camera?productId=${this.data.product.id}&releatedId=${this.data.product.releatedId}&category=${this.data.product.category}`,
      })
    }
  },

  getPhoneNumber(event) {
    console.log(event.detail.code)
  },
  onShareTimeline() {},
  
  onShareAppMessage() {
    wxdata.share(this.data.product)
    return {
      title: this.data.product.name,
      imageUrl: 'https://static.fishto.cn/images/share-thumb.png',
      path: `/pages/productDetail/productDetail?productId=${this.data.product.id}`,
    }
  },
})
